package com.bc.web.controller.org;

import com.bc.web.model.Org;
import com.bc.web.model.OrgRole;
import com.bc.web.model.OrgUser;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSMD5;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

public class OrgUserController extends FSController{
	
	public void index(){
		setAttr("sidebar", getSession().getAttribute("sidebar"));
		render("index.html");
	}
	
	public void search(){
		OrgUser orgUser = (OrgUser) super.getSession().getAttribute("orgUser");
		Page<OrgUser> items = OrgUser.dao.search(super.getSearchCondition(),orgUser.getOrgCode());
		super.setPageResultToRequest("items", items);
		render("system/orgUserList.html");
	}
	
	public void addPage(){
		OrgUser orgUser = (OrgUser) super.getSession().getAttribute("orgUser");
		setAttr("org", Org.dao.selectByOrgCode(orgUser.getOrgCode()));
		setAttr("roleList", OrgRole.dao.selectAllByOrgCode(orgUser.getOrgCode()));
		render("system/orgUserAdd.html");
	}
	
	public void updatePage(){
		OrgUser orgUser = (OrgUser) super.getSession().getAttribute("orgUser");
		OrgUser user = OrgUser.dao.findById(getPara("id"));
		setAttr("org", Org.dao.selectByOrgCode(orgUser.getOrgCode()));
		setAttr("roleList", OrgRole.dao.selectAllByOrgCode(orgUser.getOrgCode()));
		setAttr("item", user);
		render("system/orgUserUpdate.html");
	}
	
	public void add(){
		OrgUser currentLoginUser = (OrgUser) super.getSession().getAttribute("orgUser");
		OrgUser user = getModel(OrgUser.class,"item",true);
		if(OrgUser.dao.selectByUserName(user.getUserName()) != null){
			renderFail("用户名已被使用.");
		}else{
			user.setUpdateTime(FSDate.getDateTime());
			user.setPassword(FSMD5.codeFor(user.getPassword()));
			user.setPayPassword(user.getPassword());
			user.setOrgCode(currentLoginUser.getOrgCode());
			user.setEnable(true);
			user.setIsAdmin(false);
			user.save();
			renderSuccess();
		}
	}
	
	public void update(){
		OrgUser user = getModel(OrgUser.class,"item",true);
		String newPassword = getPara("newPassword");
		if(StrKit.notBlank(newPassword)){
			user.setPassword(FSMD5.codeFor(newPassword));
		}
		user.setUpdateTime(FSDate.getDateTime());
		user.update();
		renderSuccess();
	}
	
	public void delete(){
		OrgUser.dao.deleteById(getPara("id"));
		renderSuccess(false);
	}
	
	public void changeEnable(){
		OrgUser.dao.changeEnable(getParaToInt("id"));
		renderSuccess(false);
	}
	
	public void changePasswordPage(){
		render("changePassword.html");
	}
	
	public void changePassword(){
		String userId = getPara("userId");
		String oldPassword = getPara("oldPassword");
		String newPassword = getPara("newPassword");
		OrgUser user = OrgUser.dao.findById(userId);
		if(user.getPassword().equalsIgnoreCase(FSMD5.codeFor(oldPassword))){
			user.setPassword(FSMD5.codeFor(newPassword));
			user.update();
			renderSuccess("修改密码成功.",false);
		}else{
			renderFail("旧密码错误.");
		}
	}
	
	public void changePayPasswordPage(){
		render("changePayPassword.html");
	}
	
	public void changePayPassword(){
		String userId = getPara("userId");
		String oldPassword = getPara("oldPassword");
		String newPassword = getPara("newPassword");
		OrgUser user = OrgUser.dao.findById(userId);
		if(user.getPayPassword().equalsIgnoreCase(FSMD5.codeFor(oldPassword))){
			user.setPayPassword(FSMD5.codeFor(newPassword));
			user.update();
			renderSuccess("修改密码成功.",false);
		}else{
			renderFail("旧支付密码错误.");
		}
	}

}
